//
//  CZVisitorView.swift
//  swift微博01
//
//  Created by 张文文 on 15/12/16.
//  Copyright © 2015年 张文文. All rights reserved.
//

import UIKit

//协议
protocol  CZVisitorViewDelegate : NSObjectProtocol{

    //默认是必须实现的方法
    //登录按钮点击
    func visitorViewLoginClick()
}

//懒加载所有控件
//添加所有控件
//设置位置
class CZVisitorView: UIView {

    //添加代理属性
    weak var delegate : CZVisitorViewDelegate?
    override init(frame: CGRect) {
        super.init(frame: frame)
        backgroundColor = UIColor(white: 237 / 255.0, alpha: 1)
        prepareUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //轮转动画
    func rotationAnimation(){
    
        let rotation = CABasicAnimation(keyPath: "transform.rotation")
        //设置动画参数
        rotation.toValue = 2 * M_PI
        rotation.duration = 20
        rotation.repeatCount = MAXFLOAT
        //当不可见的时候 系统会认为动画完成 在完成的时候不要移除动画
        rotation.removedOnCompletion = false
        //开始核心动画
        iconView.layer.addAnimation(rotation, forKey: "rotation")
    }
    func pauseAnimation(){
    
        //纪录暂停时间
        let pauseTime = iconView.layer.convertTime(CACurrentMediaTime(), fromLayer: nil)
        //设置动画速度为0
        iconView.layer.speed = 0
        //设置动画偏移时间
        iconView.layer.timeOffset = pauseTime
    }
    func resumeAnimation(){
    
        //获取暂停时间
        let pauseTime = iconView.layer.timeOffset
        //设置动画速度为1
        iconView.layer.speed = 1
        iconView.layer.timeOffset = 0
        iconView.layer.beginTime = 0
        
        let timeSincePause = iconView.layer.convertTime(CACurrentMediaTime(), fromLayer: nil ) - pauseTime
        iconView.layer.beginTime = timeSincePause
    }
    //设置视图信息
    func setupInfo(imageName : String,message : String){
    
        iconView.image = UIImage(named: imageName)
        messageLable.text = message
//        messageLable.numberOfLines = 0
//        messageLable.sizeToFit()
        
        //隐藏小房子
        homeView.hidden = true
        //隐藏遮罩
        coverView.hidden = true
        
    }
    
    //准备界面
    func prepareUI(){
    
        //添加控件
        addSubview(iconView)
        //添加遮罩层
        addSubview(coverView)
        addSubview(homeView)
        addSubview(messageLable)
        addSubview(resgisterButton)
        addSubview(loginButton)
        //autoresizing影响Autolayout的约束
        iconView.translatesAutoresizingMaskIntoConstraints = false
        
        coverView.translatesAutoresizingMaskIntoConstraints = false
        
        homeView.translatesAutoresizingMaskIntoConstraints = false
        messageLable.translatesAutoresizingMaskIntoConstraints = false
        resgisterButton.translatesAutoresizingMaskIntoConstraints = false
        loginButton.translatesAutoresizingMaskIntoConstraints = false
        
        //设置转轮的约束centerX
        self.addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        //设置转轮的约束centerY
        self.addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: -40))
        
        //设置房子的约束
        self.addConstraint(NSLayoutConstraint(item: homeView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        self.addConstraint(NSLayoutConstraint(item: homeView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: 0))
        
        //设置消息的约束
        self.addConstraint(NSLayoutConstraint(item: messageLable, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
        self.addConstraint(NSLayoutConstraint(item: messageLable, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        self.addConstraint(NSLayoutConstraint(item: messageLable, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 226))
//        self.addConstraint(NSLayoutConstraint(item: messageLable, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 100))
//        self.addConstraint(NSLayoutConstraint(item: messageLable, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 30))
        
        //设置注册按钮的约束
        self.addConstraint(NSLayoutConstraint(item: resgisterButton, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: messageLable, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
        self.addConstraint(NSLayoutConstraint(item: resgisterButton, attribute: NSLayoutAttribute.Left, relatedBy: NSLayoutRelation.Equal, toItem: messageLable, attribute: NSLayoutAttribute.Left, multiplier: 1, constant: 0))
        self.addConstraint(NSLayoutConstraint(item: resgisterButton, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 100))
        self.addConstraint(NSLayoutConstraint(item: resgisterButton, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 30))
        
        //设置登录按钮的约束
        self.addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: messageLable, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
        self.addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Right, relatedBy: NSLayoutRelation.Equal, toItem: messageLable, attribute: NSLayoutAttribute.Right, multiplier: 1, constant: 0))
        self.addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 100))
        self.addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 30))
        
        //设置遮罩层的约束
        self.addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Top, multiplier: 1, constant: 0))
        self.addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: resgisterButton, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0))
        self.addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.Left, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Left, multiplier: 1, constant: 0))
        self.addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.Right, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Right, multiplier: 1, constant: 0))
    }
    
    
    
    
    
    
    //懒加载 转轮
    private lazy var iconView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    //懒加载 房子
    private lazy var homeView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    
    //懒加载遮罩
    private lazy var coverView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    //加载消息lable
    private lazy var messageLable : UILabel = {
    
       let messLable = UILabel()
        messLable.text = "立刻关注所有人 了解他们的最近动态"
        messLable.textColor = UIColor.redColor()
        //设置文字内容分行
        messLable.numberOfLines = 0
        //根据文本来适应大小
        messLable.sizeToFit()
        return messLable
    }()
    
    //懒加载 注册按钮
    private lazy var resgisterButton : UIButton = {
    
       let resButton = UIButton()
        resButton.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        resButton.setTitle("注册", forState: UIControlState.Normal)
        resButton.setTitleColor(UIColor.darkGrayColor(), forState: UIControlState.Normal)
        return resButton
    }()
    
    //懒加载 登陆按钮
    private lazy var loginButton : UIButton = {
    
        let logButton = UIButton()
        logButton.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        logButton.setTitle("登陆", forState: UIControlState.Normal)
        logButton.setTitleColor(UIColor.darkGrayColor(), forState: UIControlState.Normal)
        //设置文本大小
        logButton.titleLabel?.font = UIFont.systemFontOfSize(14)
        //g根据内容适应大小
        logButton.sizeToFit()
        //添加点击事件
        logButton.addTarget(self, action: "loginClick", forControlEvents: UIControlEvents.TouchUpInside)
        return logButton
    }()
}
